package eproject.mm.com.projectsupervision.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.activity.PlanNodeActivity;
import eproject.mm.com.projectsupervision.adapter.BaseRecyclerViewAdapter;
import eproject.mm.com.projectsupervision.tree.MyNodeViewFactory;
import eproject.mm.com.projectsupervision.util.HttpUtil;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.header.StoreHouseHeader;
import me.texy.treeview.TreeNode;
import me.texy.treeview.TreeView;

import static android.app.Activity.RESULT_OK;

/**
 * Created by momotea on 2018/3/19.
 */

public class ProjectInfoFragment extends LazyFragment {

    RecyclerView rv_list;
    BaseRecyclerViewAdapter adapter;

    int page = 1;
    String type;
    String projectId,projectName,projectPercentdone;

    LoadingDialog loadingDialog = null;
    //private PtrFrameLayout ptrFrameLayout;

    LinearLayout ll_treeContainer;

    TextView tv_projectName,tv_rate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        if(getArguments() != null){
            type = getArguments().getString("type","");
            projectId = getArguments().getString("projectId","");
            projectName = getArguments().getString("projectName","");
            projectPercentdone = getArguments().getString("prjPercentdone","");
        }
        return inflater.inflate(R.layout.fragment_project_info, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //ptrFrameLayout = (PtrFrameLayout)getView().findViewById(R.id.ptrFL_list);
        ll_treeContainer = (LinearLayout)getView().findViewById(R.id.ll_treeContainer);
        tv_projectName = (TextView)getView().findViewById(R.id.tv_projectName);
        tv_rate = (TextView)getView().findViewById(R.id.tv_rate);
        tv_projectName.setText(projectName);
        tv_rate.setText(projectPercentdone+"%");
        //createTree();

//        final StoreHouseHeader header = new StoreHouseHeader(getContext());
//        header.setPadding(0, 15, 0, 0);
//        header.initWithString("loading...");
//        header.setTextColor(Color.GRAY);
//        ptrFrameLayout.setHeaderView(header);
//        ptrFrameLayout.addPtrUIHandler(header);

//        ptrFrameLayout.setPtrHandler(new PtrDefaultHandler() {
//            //在这里进行数据加载
//            public void onRefreshBegin(PtrFrameLayout frame) {
//                createTree();
//            }
//        });
    }

    private void createTree(){
        loadingDialog = new LoadingDialog(getActivity());
        loadingDialog.setLoadingText("加载中...").show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String scheduleStr = HttpUtil.get("/getScheduleList?projectid="+projectId);

                    Gson gson=new Gson();
                    Map resultMap = gson.fromJson(scheduleStr, Map.class);
                    if (resultMap != null && (boolean) resultMap.get("success")) {
                        List rows = (List) resultMap.get("rows");

                        TreeNode root = TreeNode.root();
                        addSubTreeNode(root, rows, 0);

                        handler.sendMessage(handler.obtainMessage(1, root));
                    }else{
                        handler.sendMessage(handler.obtainMessage(2, null));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    handler.sendMessage(handler.obtainMessage(2, null));
                }
            }
        }).start();
    }

    private void addSubTreeNode(TreeNode parentNode, List subList,int level){
        for(int i=0;i<subList.size();i++){
            Map itemMap = (Map)subList.get(i);

            String id = (String)itemMap.get("id");
            String name = (String)itemMap.get("name");
            String percentdone = (String)itemMap.get("percentdone");
            List subNodes = (List)itemMap.get("childrens");

            TreeNode subNode = new TreeNode(itemMap);
            //subNode.setValue(id);
            subNode.setLevel(level);

            if(subNodes!=null && subNodes.size()>0){
                subNode.setExpanded(false);
                addSubTreeNode(subNode, subNodes, level+1);
            }else{
                //subNode.setItemClickEnable(true);
            }

            parentNode.addChild(subNode);
        }
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    TreeNode root = (TreeNode)msg.obj;
                    View treeView = new TreeView(root, getActivity(), new MyNodeViewFactory(handler)).getView();
                    treeView.setLayoutParams(new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    ll_treeContainer.addView(treeView);
                    loadingDialog.close();

                    break;
                case 2:
                    loadingDialog.close();
                    break;
                case 5:
                    Map nodeMap = (Map)msg.obj;
                    String id = (String)nodeMap.get("id");
                    String name = (String)nodeMap.get("name");

                    Intent intent=new Intent();

                    Bundle bundle = new Bundle();
                    bundle.putString("id",id);
                    bundle.putString("name",name);

                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    };

    public void LazyLoad(){
        createTree();
    }
}
